<?php


namespace App\Middleware;


use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Exception\UnauthorizedHttpException;
use HyperfExt\Auth\Contracts\AuthManagerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ApiTokenAuthMiddleware implements MiddlewareInterface
{

    /**
     * @var RequestInterface
     */
    protected $request;

    /**
     * @var HttpResponse
     */
    protected $response;
    /**
     * @Inject()
     * @var AuthManagerInterface
     */
    private $authManager;
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        if (!$this->authManager->guard('token')->check()) {
            return $this->response->json([
                'code'    => 401,
                'message' => 'Token authentication does not pass !',
            ]);
        }

        return $handler->handle($request);
        // TODO: Implement process() method.
    }
}